package com.tang.thepath;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.view.ImageUploader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class RepairActivity extends BaseActivity {

    private EditText editCompanyName;
    private EditText editAddress;
    private EditText editContact;
    private EditText editIssueTitle;
    private EditText editReason;
    private EditText editCarModel;
    private EditText editSaleTime;
    private Button btnSave;
    private ImageUploader imageUploader;


    int mYear;
    int mMonth;
    int mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair);

        editCompanyName = (EditText) findViewById(R.id.edit_company);
        editAddress = (EditText) findViewById(R.id.edit_address);
        editContact = (EditText) findViewById(R.id.edit_contact);
        editIssueTitle = (EditText) findViewById(R.id.issue_title);
        editReason = (EditText) findViewById(R.id.reason);
        editCarModel = (EditText) findViewById(R.id.car_model);
        editSaleTime = (EditText) findViewById(R.id.sale_time);
        imageUploader = (ImageUploader) findViewById(R.id.img_uploader);
        btnSave = (Button) findViewById(R.id.btn_save);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Const.NETWORK_MOD == 0){
                    toast("请检查您的网络设置");
                    return;
                }

                String companyName = editCompanyName.getText().toString();
                String address = editAddress.getText().toString();
                String contact = editContact.getText().toString();
                String issueTitle = editIssueTitle.getText().toString();
                String reason = editReason.getText().toString();
                String carModel = editCarModel.getText().toString();
                String saleTime = editSaleTime.getText().toString();

                if(companyName.isEmpty()){
                    toast("公司名称不能为空");
                    return;
                }

                if(contact.isEmpty()){
                    toast("联系人不能为空");
                    return;
                }

                if(issueTitle.isEmpty()){
                    toast("故障设备名称不能为空");
                    return;
                }

                if(carModel.isEmpty()){
                    toast("车辆型号不能为空");
                    return;
                }

                if(saleTime.isEmpty()){
                    toast("销售时间不能为空");
                    return;
                }

                final RequestModel.SaveRepairRequest request = new RequestModel.SaveRepairRequest();
                request.companyName = companyName;
                request.address = address;
                request.contact = contact;
                request.issueTitle = issueTitle;
                request.reason = reason;
                request.carModel = carModel;

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    request.saleTime = sdf.parse(saleTime).getTime()/1000;
                } catch (ParseException e) {
                    e.printStackTrace();
                    request.saleTime = 0;
                }

                //上传图片
                if(imageUploader.imageCount() > 0){
                    showLoading("正在上传图片");
                    imageUploader.uploadImages().subscribe(new HttpSubscriber<RequestModel.UploadResponse>() {
                        @Override
                        protected void onSuccess(RequestModel.UploadResponse uploadResponse) {
                            dismisLoading();
                            request.images = uploadResponse.pathList;
                            saveRepair(request);
                        }
                    });
                }else{
                    request.images = new ArrayList<>();
                    saveRepair(request);
                }
            }
        });


        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        editSaleTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog dialog = new DatePickerDialog(RepairActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        mYear = year;
                        mMonth = month;
                        mDay = dayOfMonth;
                        editSaleTime.setText(mYear+"-"+mMonth+"-"+mDay);
                    }
                },mYear,mMonth,mDay);
                dialog.show();
            }
        });
    }

    protected void saveRepair(RequestModel.SaveRepairRequest request){
        showLoading("数据提交中");
        BizService.saveRepair(request).subscribe(new HttpSubscriber<RequestModel.SaveRepairResponse>() {
            @Override
            protected void onSuccess(RequestModel.SaveRepairResponse saveRepairResponse) {
                String message = "存储成功:"+saveRepairResponse.id;
                Log.i(getTag(),message);
                toast(message);
                dismisLoading();
                finish();
            }
        });
    }

    @Override
    public ImageUploader getImageUploader() {
        return imageUploader;
    }
}
